package com.tran.app.viewmodel

import android.text.TextUtils
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import com.orhanobut.logger.Logger
import com.tran.app.MyApp
import com.tran.app.dao.AppDatabase
import com.tran.app.data.LoginUi
import com.tran.app.data.RegisterUi
import com.tran.app.data.Teacher
import com.tran.app.data.UserInfo
import com.tran.app.utils.show
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginViewModel : ViewModel() {

     val _uidata = MutableStateFlow(LoginUi(false,"",""))
    val uidata: StateFlow<LoginUi> = _uidata.asStateFlow()

    fun setui(ui: LoginUi) {
        _uidata.update {
            it.copy(
                name = ui.name,
                work = ui.work,
                isloading = ui.isloading,
                pw = ui.pw
            )
        }
    }

    fun login(navController:NavController){
        if(TextUtils.isEmpty(uidata.value.name)){
            show("用户名输入为不能为空")
            return
        }
        if(TextUtils.isEmpty(uidata.value.pw)){
            show("密码不能为空")
            return
        }
        if(TextUtils.isEmpty(uidata.value.work)){
            show("身份不能为空")
            return
        }
        if(uidata.value.work=="学生"){
            viewModelScope.launch {
                var u: UserInfo?
                withContext(Dispatchers.IO){
                     u = AppDatabase.getInstance(MyApp.instance).userInfoDao().getUserByName(uidata.value.name)
                }
                withContext(Dispatchers.Main) {
                    if (u == null) {
                        show("用户名不存在")
                    } else {
                        if(uidata.value.pw== u!!.pw){
                            MyApp.user = u!!.name
                            MyApp.realName = u!!.realName
                            MyApp.work = uidata.value.work
                            show("登录成功")
                            navController.navigate("main/${u!!.id}")
                        }else{
                            show("密码错误")
                        }
                    }
                }
            }
        }else if(uidata.value.work=="老师"){
            viewModelScope.launch {
                var u:Teacher?
                Logger.d(uidata.value.name)
                withContext(Dispatchers.IO){
                     u = AppDatabase.getInstance(MyApp.instance).teacherDao().getTeacherByAccount(uidata.value.name)
                }
                withContext(Dispatchers.Main) {
                    if (u == null) {
                        show("用户名不存在")
                    } else {
                        if(uidata.value.pw== u!!.password){
                            MyApp.user = u!!.account
                            MyApp.realName = u!!.name
                            MyApp.work = uidata.value.work
                            show("登录成功")
                            navController.navigate("maintech")
                        }else{
                            show("密码错误")
                        }
                    }
                }
            }
        }else{
            show("身份不合法")
        }
    }
}